var geocoder;
var address="";
var map;
var contentString;
var i;
function initialize() {
	var styles = [
	              {
	            	  stylers: [
	            	            { hue: "#00ffe6" },
	            	            { saturation: -20 }
	            	            ]
	              },{
	            	  featureType: "road",
	            	  elementType: "geometry",
	            	  stylers: [
	            	            { lightness: 100 },
	            	            { visibility: "simplified" }
	            	            ]
	              },{
	            	  featureType: "road",
	            	  elementType: "labels",
	            	  stylers: [
	            	            { visibility: "off" }
	            	            ]
	              }
	              ];
	geocoder = new google.maps.Geocoder();
	var myLatlng=new google.maps.LatLng(47, 2);
	var styledMap = new google.maps.StyledMapType(styles,
			{name: "Styled Map"});
	var mapOptions = {
			zoom: 3,
			center: myLatlng,
			mapTypeControlOptions: {
				mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
			}
	};
	map = new google.maps.Map(document.getElementById('map-canvas'),
			mapOptions);
	map.mapTypes.set('map_style', styledMap);
	map.setMapTypeId('map_style');
	address="0 Cours Intendance 33000 BORDEAUX";
	$.when( $.ajax( "./RetourAjax/GetAdress.php" ) ).then(function( data, textStatus, jqXHR ) {
		AddMarkers(JSON.parse(data));
	});
}
function AddMarkers(data){
	for(i=0;i<data.length;i++){
			codeAddress(data[i]);
	}
}

function codeAddress(data) {

		vretour="";
		var marker = new google.maps.Marker({
			map: map,
			animation: google.maps.Animation.DROP,
			position: data.coordonnees
		});
		var infowindow = new google.maps.InfoWindow({
			content: data.info
		});
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
		});
		return vretour;
	
}
$(window).ready(function(){
	$('.updateCoord').click(function(){
		$.ajax({
			  url: "./MAJGMap.php",
			  beforeSend: function() {
			    $('.updateCoord').append('<span class="glyphicon glyphicon-refresh load" aria-hidden="true"></span>');
			  },
			  success: function(html) {
				bootbox.alert("Coordonnées chargées");
			    $('.load').remove();
			    $(window).bind('beforeunload', function(){ 
					location.refresh();
				});
			  }
			});
		$(window).bind('beforeunload', function(){ 
			return 'Chargement des coordonnées en cours...';
		});
	});
	google.maps.event.addDomListener(window, 'load', initialize);
})
